﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.Command;
using System.Data;
using System.IO;
using Model;
using System.Configuration;

namespace CO9APPFORM
{
    public partial class CandidateInfoList : BasePage
    {
        private APP_PERSONAL DSPersonal
        {
            get
            {
                APP_PERSONAL result = new APP_PERSONAL();

                if (ViewState["Personal"] != null)
                {
                    result = (APP_PERSONAL)ViewState["Personal"];
                }

                return result;
            }
            set
            {
                ViewState["Personal"] = value;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            NeedAllowRole("Recruit", "Default.aspx");

            if (!Page.IsPostBack)
            {
                LoadPersonalInfo();
            }
        }

        private void LoadPersonalInfo()
        {
            var P = new GetPersonalInfoListCommand();
            P.Command();

            if (P.Result != null && P.Result.Count != 0)
            {
                gvCandidate.DataSource = P.Result.ToList();
                gvCandidate.DataBind();

                gvCandidate.Visible = true;
            }
            else
            {
                gvCandidate.Visible = false;
                dvNotFound.Visible = true;
            }
        }

        protected void gvCandidate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[3].Text != null || e.Row.Cells[3].Text != string.Empty)
                {
                    if (e.Row.Cells[3].Text == "5")
                    {
                        e.Row.Cells[3].Text = "Success";
                        e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;

                        LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete");
                        btnDelete.Visible = false;
                    }
                    else
                    {
                        e.Row.Cells[3].Text = "Unsuccess";
                        e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }

        protected void gvCandidate_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditApplication")
            {
                Response.Redirect("Application.aspx?Hash=" + e.CommandArgument);
            }
            else if (e.CommandName == "DeleteApplication")
            {
                var D = new GetPersonalInfoCommand(e.CommandArgument.ToString());
                D.Execute();
                DSPersonal = D.Result;

                if (DSPersonal.IMG != null && DSPersonal.IMG != "")
                {
                    string PathAndFile = Server.MapPath(ConfigurationManager.AppSettings["UploadPath"].ToString()) + DSPersonal.IMG;
                    if (File.Exists(PathAndFile))
                    {
                        File.Delete(PathAndFile);
                    }
                }

                var P = new MaintainPersonalInfo(e.CommandArgument.ToString());
                P.Execute();

                LoadPersonalInfo();
            }
            else //Send Email
            {
                Response.Redirect("CandidateInfo.aspx?hash=" + e.CommandArgument.ToString());
            }

        }

        protected void gvCandidate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCandidate.PageIndex = e.NewPageIndex;
            LoadPersonalInfo();
        }
    }
}